
<div class="container bg-light"  style="max-width:500px; margin: 20px auto;">
    <div class="row d-flex align-items-center justify-content-center">

        <div style="width:360px;" class="my-4">

            <form method="post" id="form-login">
                <h3 class="mb-3">用户登录</h3>
                <div class="form-group mb-3">
                    <input type="text" name="username" class="form-control" placeholder="用户名或Email" required>
                </div>
                <div class="form-group mb-3">
                    <input type="password" name="password" class="form-control" placeholder="密码" required>
                </div>
                <div class="form-group d-flex justify-content-between mb-3">
                    <input type="text" name="image_code" class="form-control w-50" autocomplete="off" placeholder="验证码" required>
                    <img class="rounded" src="/app/user/captcha/image/login"/>
                </div>
                <div class="form-group mb-3">
                    <button type="submit" class="btn btn-primary btn-block w-100">登录</button>
                </div>
                <div class="form-group d-flex justify-content-between">
                    <a class="text-decoration-none" id="registerLink" href="/app/user/register">没有账号？点这里注册</a>
                    <a class="text-decoration-none" href="/app/user/password/reset">找回密码</a>
                </div>
            </form>
        </div>


    </div>

    <script>
        $('#form-login img').on('click', function (e) {
            e.stopPropagation();
            e.preventDefault();
            $(this).attr('src', '/app/user/captcha/image/login?r='+ Math.random());
            $('input[name="image_code"]').val('');
        });
    
        $('input').keyup(function () {
            $(this).removeClass('is-invalid');
        });
    
        $('#form-login').submit(function(event) {
            event.preventDefault();
            $.ajax({
                url: "/app/laoha/user/login",
                type: "POST",
                dataType: 'json',
                data: $(this).serialize(),
                success: function (e) {
                    if (e.code !== 0) {
                        let field = e.data ? e.data.field : false;
                        field !== 'image_code' && $('#form-login img').trigger('click');
                        field && $('input[name="'+field+'"]').addClass('is-invalid').focus();
                        return alert(e.msg);
                    }
                    location.reload();
                }
            });
        });
    
        $("#registerLink").attr('href', '/app/user/register' + location.search);
    
    </script>
</div>

